package com.yaozheng.vocationaltraining.view;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.adapter.HomeAnswerTaskAdapter;
import com.yaozheng.vocationaltraining.dialog.HomeAnswerTaskDialogFragment;
import com.yaozheng.vocationaltraining.service.HomeAnswerTaskService;
import com.yaozheng.vocationaltraining.utils.QuestionUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * 答题View
 * 
 * @author Administrator
 * 
 */
@EViewGroup(R.layout.view_dialog_home_answer_task)
public class HomeAnswerTaskView extends LinearLayout {
//	private HomeAnswerTaskDialogFragment homeAnswerTaskDialog;

	@ViewById
	ViewPager viewPager;

	HomeAnswerTaskAdapter homeAnswerTaskAdapter;

	JSONObject paperJsonObject;

//	HomeAnswerTaskService homeAnswerTaskService;



	public HomeAnswerTaskView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public HomeAnswerTaskView(Context context) {
		super(context);
	}

	public HomeAnswerTaskView(Context context,
			HomeAnswerTaskDialogFragment homeAnswerTaskDialog) {
		super(context);
//		this.homeAnswerTaskDialog = homeAnswerTaskDialog;
	}

//	public void setHomeAnswerTaskService(
//			HomeAnswerTaskService homeAnswerTaskService) {
//		this.homeAnswerTaskService = homeAnswerTaskService;
//	}

	@AfterViews
	public void initView() {

		// showCurrFragment(PaperQuestionListFragment_.builder().build(),
		// false;

		// showCurrFragment(NewsMenuFragment_.builder().build(), false);
	}

	public void loadData(JSONObject paperJsonObject) {
		this.paperJsonObject = paperJsonObject;
		JSONArray questionArray = TypeUtils.getJsonArray(paperJsonObject,
				"questions");
		if (homeAnswerTaskAdapter == null) {
			homeAnswerTaskAdapter = new HomeAnswerTaskAdapter(getContext(),
					questionArray);
			viewPager.setAdapter(homeAnswerTaskAdapter);
		} else {
			homeAnswerTaskAdapter.getDataList().clear();
			homeAnswerTaskAdapter.getDataList().addAll(questionArray);
			homeAnswerTaskAdapter.notifyDataSetChanged();
		}

	}

//	@Click(R.id.cancelText)
//	public void cancelClick() {
//		if (paperJsonObject != null) {
//			JSONArray questionArray = TypeUtils.getJsonArray(paperJsonObject,
//					"questions");
//			JSONObject answers = QuestionUtils.getAnswers(questionArray);
//			if (answers != null) {
//				if (homeAnswerTaskService != null) {
//					homeAnswerTaskService.submitAnswer(
//							TypeUtils.getJsonString(paperJsonObject, "id"),
//							answers.toString());
//					return;
//				}
//			}
//		}
//
////		homeAnswerTaskDialog.cancel();
//	}


	/**
	 * 初始化
	 */
	public void onFinishInflate() {
		super.onFinishInflate();
	}

}
